import redis,time
res = redis.Redis(host='localhost', port=6379,db=1)
class RedlsLock(object):
    def __init__(self,key):
        self.key=f'lock{key}'
        self.lock_timeout=10 #  锁持有时间限制为10秒

    def acquire(self):
        while True:
            #  尝试加锁
            if res.setnx(self.key,1):

                res.expire(self.key,self.lock_timeout)
                return True
            else:
                # 已经有其他节点持有该锁，等待重试
                time.sleep(1)
    def release(self):
        res.delete(self.key)
r=RedlsLock


key = 'askjfakjg'
flag = res.setnx(key+'setnx',1)
if flag == True:
    res.delete(key+'setnx')
